from WidgetElementsParams.ChooseElementWidget import WindowElemParams
from . ui_fluid_settings import Ui_Dialog

def render_window_errors(window: WindowElemParams,
                         current_elem_settings: dict):
    window.choose_type_elem_cb.setCurrentText(current_elem_settings['type_element'])
    type_element = window.choose_type_elem_cb.currentText()
    if type_element == 'Прямой участок' or type_element == '':
        window.straight_pipe.choose_type_cross_sec_cb.setCurrentText(current_elem_settings['cross_section'])
        cross_section = window.straight_pipe.choose_type_cross_sec_cb.currentText()
        if cross_section == 'Круглое' or cross_section == '':
            window.straight_pipe.circle_pipe.length.setText(current_elem_settings['length'])
            window.straight_pipe.circle_pipe.diameter.setText(current_elem_settings['diameter'])
        elif cross_section == 'Прямоугольное':
            window.straight_pipe.rect_pipe.length.setText(current_elem_settings['length'])
            window.straight_pipe.rect_pipe.width.setText(current_elem_settings['width'])
            window.straight_pipe.rect_pipe.height.setText(current_elem_settings['height'])

    elif type_element == 'Изогнутый участок' or type_element == '':
        window.curve_pipe.choose_type_cross_sec_cb.setCurrentText(current_elem_settings['cross_section'])
        cross_section = window.curve_pipe.choose_type_cross_sec_cb.currentText()
        if cross_section == 'Круглое' or cross_section == '':
            window.curve_pipe.circle_pipe.diameter.setText(current_elem_settings['diameter'])
            window.curve_pipe.circle_pipe.bend_radius.setText(current_elem_settings['bend_radius'])
            window.curve_pipe.circle_pipe.angle.setText(current_elem_settings['angle'])

        elif cross_section == 'Прямоугольное':
            window.curve_pipe.rect_pipe.bend_radius.setText(current_elem_settings['bend_radius'])
            window.curve_pipe.rect_pipe.angle.setText(current_elem_settings['angle'])
            window.curve_pipe.rect_pipe.height.setText(current_elem_settings['height'])
            window.curve_pipe.rect_pipe.width.setText(current_elem_settings['width'])


    elif type_element == 'Угольник' or type_element == '':
        window.elbow.choose_type_cross_sec_cb.setCurrentText(current_elem_settings['cross_section'])
        cross_section = window.elbow.choose_type_cross_sec_cb.currentText()
        if cross_section == 'Круглое' or cross_section == '':
            window.elbow.elbow_circle.diameter.setText(current_elem_settings['diameter'])
            window.elbow.elbow_circle.angle.setText(current_elem_settings['angle'])

    elif type_element == '':
        window.straight_pipe.choose_type_cross_sec_cb.setCurrentText(current_elem_settings['cross_section'])
        cross_section = window.straight_pipe.choose_type_cross_sec_cb.currentText()
        if cross_section == 'Круглое':
            window.straight_pipe.circle_pipe.length.setText(current_elem_settings['length'])
            window.straight_pipe.circle_pipe.radius.setText(current_elem_settings['diameter'])
        elif cross_section == 'Прямоугольное':
            window.straight_pipe.circle_pipe.length.setText(current_elem_settings['length'])
            window.straight_pipe.rect_pipe.length.setText(current_elem_settings['width'])
            window.straight_pipe.rect_pipe.height.setText(current_elem_settings['height'])
    return window


def render_window_edit_elem(app,
                            window: WindowElemParams,
                            index_edit: str = None):
    if index_edit:
        sender = app.sender()
        if sender.text() == 'Редактировать элемент' and index_edit:
            params_for_edit = app.hydraulic_line.hydraulic_line[index_edit].get_params_element()
            dct_params_for_edit = {k: v for k, v in params_for_edit.items()
                                   if k in ('type',
                                            'section',
                                            'params_geom',)}
            window.choose_type_elem_cb.setCurrentText(dct_params_for_edit['type'])
            type_element = window.choose_type_elem_cb.currentText()

            if type_element == 'Прямой участок':
                window.straight_pipe.choose_type_cross_sec_cb.setCurrentText(dct_params_for_edit['section'])
                cross_section = window.straight_pipe.choose_type_cross_sec_cb.currentText()

                if cross_section == 'Круглое':

                    window.straight_pipe.circle_pipe.length.setText(
                        dct_params_for_edit['params_geom']['Длина'].split()[0])

                    window.straight_pipe.circle_pipe.diameter.setText(
                        dct_params_for_edit['params_geom']['Диаметр'].split()[0])

                elif cross_section == 'Прямоугольное':

                    window.straight_pipe.rect_pipe.length.setText(
                        dct_params_for_edit['params_geom']['Длина'].split()[0])

                    window.straight_pipe.rect_pipe.width.setText(
                        dct_params_for_edit['params_geom']['Ширина'].split()[0])

                    window.straight_pipe.rect_pipe.height.setText(
                        dct_params_for_edit['params_geom']['Высота'].split()[0])

            elif type_element == 'Изогнутый участок':
                window.curve_pipe.choose_type_cross_sec_cb.setCurrentText(dct_params_for_edit['section'])
                cross_section = window.curve_pipe.choose_type_cross_sec_cb.currentText()
                if cross_section == 'Круглое':
                    window.curve_pipe.circle_pipe.bend_radius.setText(
                        dct_params_for_edit['params_geom']['Радиус гиба'].split()[0])

                    window.curve_pipe.circle_pipe.angle.setText(
                        dct_params_for_edit['params_geom']['Угол гиба'].split()[0])

                    window.curve_pipe.circle_pipe.diameter.setText(
                        dct_params_for_edit['params_geom']['Диаметр'].split()[0])
                elif cross_section == 'Прямоугольное':
                    window.curve_pipe.rect_pipe.bend_radius.setText(
                        dct_params_for_edit['params_geom']['Радиус гиба'].split()[0])

                    window.curve_pipe.rect_pipe.angle.setText(
                        dct_params_for_edit['params_geom']['Угол гиба'].split()[0]
                    )

                    window.curve_pipe.rect_pipe.width.setText(
                        dct_params_for_edit['params_geom']['Ширина'].split()[0]
                    )

                    window.curve_pipe.rect_pipe.height.setText(
                        dct_params_for_edit['params_geom']['Высота'].split()[0]
                    )
            elif type_element == 'Угольник':
                window.elbow.choose_type_cross_sec_cb.setCurrentText(dct_params_for_edit['section'])
                cross_section = window.elbow.choose_type_cross_sec_cb.currentText()
                if cross_section == 'Круглое':
                    window.elbow.elbow_circle.angle.setText(
                        dct_params_for_edit['params_geom']['Угол гиба'].split()[0]
                    )
                    window.elbow.elbow_circle.diameter.setText(
                        dct_params_for_edit['params_geom']['Диаметр'].split()[0]
                    )

    return window

def render_window_fluid_settings(main_window, window: Ui_Dialog):
    window.name.setText(main_window.fluidname.text())
    window.density.setText(main_window.value_density.text())
    window.dynamic_viscosity.setText(main_window.value_viscos.text())
    window.mass_flow.setText(main_window.value_massflow.text())

